package acwing.search;

public class binarySearch_ {
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,2,5,6};
        System.out.println(binarySearch5(nums, 2));
    }

    // 左右闭区间
    public static int binarySearch1(int[] arr,int target){
        if (arr == null || arr.length == 0){
            return -1;
        }
        if (arr[0] > target || arr[arr.length - 1] < target){
            return -1;
        }
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if (arr[mid] == target){
                return mid;
            }
            if (arr[mid] < target){
                left = mid + 1;
            }
            else if (arr[mid] > target){
                right = mid - 1;
            }
        }
        return -1;
    }

    // 左开右闭区间
    public static int binarySearch2(int[] arr,int target){
        if (arr == null || arr.length == 0){
            return -1;
        }
        if (arr[0] > target || arr[arr.length - 1] < target){
            return -1;
        }
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int mid = left + (right - left) / 2;
            if (arr[mid] == target){
                return mid;
            }
            if (arr[mid] < target){
                left = mid;
            }
            else if (arr[mid] > target){
                right = mid - 1;
            }
        }
        return -1;
    }
    // 左闭右开区间
    public static int binarySearch3(int[] arr,int target){
        if (arr == null || arr.length == 0){
            return -1;
        }
        if (arr[0] > target || arr[arr.length - 1] < target){
            return -1;
        }
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int mid = left + (right - left) / 2;
            if (arr[mid] == target){
                return mid;
            }
            if (arr[mid] < target){
                left = mid + 1;
            }
            else if (arr[mid] > target){
                right = mid;
            }
        }
        return -1;
    }

    // y总模板 [left,right] -> [l,mid] + [mid + 1,right] 找到第一个满足条件的值
    // y总模板[left,right] -> [l, mid - 1] + [mid,right] 找到最后一个满足条件的值
    public static int binarySearch4(int[] arr,int target){
        // 剪枝
        if (arr == null || arr.length == 0)
            return -1;
        if (arr[0] > target || arr[arr.length - 1] < target)
            return -1;
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int mid = left + right >> 1;
            if (arr[mid] >= target) right = mid;
            else left = mid + 1;
        }
        return left;
    }
    // y总模板[left,right] -> [l, mid - 1] + [mid,right] 找到最后一个满足条件的值
    public static int binarySearch5(int[] arr,int target) {
        // 剪枝
        if (arr == null || arr.length == 0)
            return -1;
        if (arr[0] > target || arr[arr.length - 1] < target)
            return -1;
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int mid = left + right + 1>> 1;
            if (arr[mid] <= target)
                left = mid;
            else right = mid - 1;
        }
        return left;
    }
}
